package controller;

import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import process.*;


/**
 * Servlet implementation class Controller
 */
public class ControllerCVManagement extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String DELETE_JSP = "/Delete.jsp";
	private static String EDIT_JSP = "/Edit.jsp";
	private static String SHOWALL_JSP = "/ShowAll.jsp";

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ControllerCVManagement() 
    {
        super();
        // TODO Auto-generated constructor stub

        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
			
		HttpSession session = request.getSession();
		
		// Verifier que l'utilisateur a login 		
		 if ((String)session.getAttribute("login") == null)
		 {
			 // il n'a pas login 
			 response.sendRedirect("SignIn.jsp");	
		 }
		 else
		 {
				// caracteres en unicode
				request.setCharacterEncoding("UTF-8");					
				
				CVManagement cv = new CVManagement();			
			
				
				
				// *******************************************************//
				// *			LA TABLE CANDIDAT						 *//
				// *******************************************************//	
				Vector<String> parametre_Candidat = new Vector<String>(); 		
				
				parametre_Candidat.add(request.getParameter("name"));
				parametre_Candidat.add(request.getParameter("web_site"));
				parametre_Candidat.add(request.getParameter("address"));
				parametre_Candidat.add(request.getParameter("phone"));
				parametre_Candidat.add(request.getParameter("email"));
				parametre_Candidat.add(request.getParameter("date_birth"));
				parametre_Candidat.add(request.getParameter("salary_wanted"));
				parametre_Candidat.add(request.getParameter("levelstudy"));
				parametre_Candidat.add(request.getParameter("sex"));					
				parametre_Candidat.add(request.getParameter("description"));		
				parametre_Candidat.add(request.getParameter("languages"));
				
				// num_user de candidat			
				parametre_Candidat.add((String)session.getAttribute("num_user"));
				
				// *******************************************************//
				// *			LA TABLE EXPERIENCE						 *//
				// *******************************************************//
				Vector<String> parametre_Expericence = new Vector<String>(); 
				
				parametre_Expericence.add(request.getParameter("year_experience"));
				parametre_Expericence.add(request.getParameter("position_wanted"));
				parametre_Expericence.add(request.getParameter("domain"));
				parametre_Expericence.add(request.getParameter("specialite"));
				
				String num_Ca = (String)session.getAttribute("num_Ca");	
			
			
				if(num_Ca == null)
				{			
					cv.saveCVManagement(parametre_Candidat,parametre_Expericence);
					session.setAttribute("num_Ca",cv.getNumCa());			
				}
				else
				{			
					cv.UpdateCVManagement(Integer.parseInt(num_Ca),parametre_Candidat,parametre_Expericence);
				}
					
				//  Update les variables sessions 
		    	session.setAttribute("name", request.getParameter("name"));
		       	session.setAttribute("web_site",request.getParameter("web_site"));
		       	session.setAttribute("address",request.getParameter("address"));    
		       	session.setAttribute("phone",request.getParameter("phone"));  	
		       	session.setAttribute("email",request.getParameter("email"));  
		       	session.setAttribute("date_birth",request.getParameter("date_birth"));
		       	session.setAttribute("salary_wanted",request.getParameter("salary_wanted"));   	
		       	session.setAttribute("levelstudy",request.getParameter("levelstudy"));	       	
		    	session.setAttribute("sex",request.getParameter("sex"));		
		    	session.setAttribute("description",request.getParameter("description"));
		    	session.setAttribute("languages",request.getParameter("languages"));
		    	  	
		    	// pour l'experience 
				session.setAttribute("year_experience",request.getParameter("year_experience")); 
				session.setAttribute("position_wanted",request.getParameter("position_wanted")); 
				session.setAttribute("domain",request.getParameter("domain"));    	
				session.setAttribute("specialite",request.getParameter("specialite"));
				
				// Reload 
				response.sendRedirect("CVManagement.jsp");	
		 
		 }
		
	
	}

}
